home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7886 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  14.5 KB

  1. Path: newsfeed.internetmci.com!gatech!gt-news!james
  2. From: james@amber.biology.gatech.edu (James McIninch)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Explain this> %s \"%[^\"]\"
  5. Date: 29 Feb 1996 15:22:38 GMT
  6. Organization: Georgia Institute of Technology
  7. Distribution: world
  8. Message-ID: <4h4gbu$qbo@mordred.gatech.edu>
  9. References: <4h2t6u$v56@useneta1.news.prodigy.com>
  10. NNTP-Posting-Host: exon.biology.gatech.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. David Cunningham (DZYS46D@prodigy.com) wrote:
  14.  
  15. : I have a file that contains text like this:
  16.  
  17. : 012345678     "Joe  Smith"
  18.  
  19. : I also have an example of sscanf to pick out these fields. Can someone 
  20. : translate what  \"%[^\"]\"%d"  means. I need to have a good understanding 
  21. : of this for a homework assignment. It looks like this:
  22.  
  23. : sscanf(buff,"%s \ "%[^\"]\" %d",student_list->ss_num,student_list->name);
  24.  
  25. : Please explain what these hieroglyphics mean, character by character.
  26. : The sscanf will produce this--> 012345678  Joe Smith
  27. : Please don't email, post the answer, thanks.
  28.  
  29. Check the section of the manual regarding the format specification below:
  30.  
  31.  
  32. scanf(3S)            Standard I/O Functions             scanf(3S)
  33.  
  34. NAME
  35.      scanf, fscanf, sscanf - convert formatted input
  36.  
  37. SYNOPSIS
  38.      #include <stdio.h>
  39.  
  40.      int scanf(const char *format, ...);
  41.  
  42.      int fscanf(FILE *strm, const char *format, ...);
  43.  
  44.      int sscanf(const char *s, const char *format, ...);
  45.  
  46. MT-LEVEL
  47.      MT-Safe
  48.  
  49. DESCRIPTION
  50.      scanf() reads from the standard input stream, stdin.
  51.  
  52.      fscanf() reads from the stream strm.
  53.  
  54.      sscanf() reads from the character string s.
  55.  
  56.      Each function reads characters, interprets them according to
  57.      a  format,  and  stores  the results in its arguments.  Each
  58.      expects, as arguments, a control string,  format,  described
  59.      below  and  a  set of pointer arguments indicating where the
  60.      converted input should be stored. If there are  insufficient
  61.      arguments  for the format, the behavior is undefined. If the
  62.      format is exhausted while arguments remain, the excess argu-
  63.      ments are simply ignored.
  64.  
  65.      The control string usually  contains  conversion  specifica-
  66.      tions,  which  are  used  to  direct interpretation of input
  67.      sequences.  The control string may contain:
  68.  
  69.           1.  White-space characters (blanks, tabs, new-lines, or
  70.               form-feeds)  that,  except  in  two cases described
  71.               below, cause input to be read up to the  next  non-
  72.               white-space character.
  73.  
  74.           2.  An ordinary character (not %) that must  match  the
  75.               next character of the input stream.
  76.  
  77.           3.  Conversion specifications consisting of the charac-
  78.               ter  %  or  the  character  sequence  %digits$,  an
  79.               optional assignment  suppression  character  * ,  a
  80.               decimal  digit  string  that  specifies an optional
  81.               numerical maximum field width, an optional letter l
  82.               (ell), L, or h indicating the size of the receiving
  83.               object, and a conversion code:
  84.               % or digit, *, decimal digit string, h or l  or  L,
  85.               conversion code
  86.  
  87.           A conversion specification directs  the  conversion  of
  88.           the next input field; the result is placed in the vari-
  89.           able pointed to by the  corresponding  argument  unless
  90.           assignment suppression was indicated by the character *
  91.           .  The suppression of  assignment  provides  a  way  of
  92.           describing  an  input  field that is to be skipped.  An
  93.           input field is defined as a string of non-space charac-
  94.           ters; it extends to the next inappropriate character or
  95.           until the maximum  field width, if one is specified, is
  96.           exhausted.   For all descriptors except the character [
  97.           and the character c, white space leading an input field
  98.           is ignored.
  99.  
  100.           Conversions can be applied to the nth argument  in  the
  101.           argument list, rather than to the next unused argument.
  102.           In this case, the conversion character % (see above) is
  103.           replaced  by  the  sequence  %digits$ where digits is a
  104.           decimal integer n, giving the position of the  argument
  105.           in  the  argument  list.  The first such argument, %1$,
  106.           immediately follows format.   The  control  string  can
  107.           contain either form of a conversion specification, that
  108.           is, % or %digits$, although the  two  forms  cannot  be
  109.           mixed within a single control string.
  110.  
  111.           The conversion code indicates the interpretation of the
  112.           input  field;  the  corresponding pointer argument must
  113.           usually be of a  restricted  type.   For  a  suppressed
  114.           field,  no  pointer  argument  is given.  The following
  115.           conversion codes are valid:
  116.  
  117.           %    A single % is expected in the input at this point;
  118.                no assignment is done.
  119.  
  120.           d    Matches  an  optionally  signed  decimal  integer,
  121.                whose  format is the same as expected for the sub-
  122.                ject sequence of the strtol()  function  with  the
  123.                value 10 for the base argument.  The corresponding
  124.                argument should be a pointer to integer.
  125.  
  126.           u    Matches  an  optionally  signed  decimal  integer,
  127.                whose  format is the same as expected for the sub-
  128.                ject  sequence  of  the  strtoul()  function  (see
  129.                strtol(3C))  with  the value 10 for the base argu-
  130.                ment.  The  corresponding  argument  should  be  a
  131.                pointer to unsigned integer.
  132.  
  133.           o    Matches an optionally signed octal integer,  whose
  134.                format  is  the  same  as expected for the subject
  135.                sequence of the strtoul() function with the  value
  136.                8  for the base argument.  The corresponding argu-
  137.                ment should be a pointer to unsigned integer.
  138.  
  139.           x    Matches an optionally signed hexadecimal  integer,
  140.                whose  format is the same as expected for the sub-
  141.                ject sequence of the strtoul() function  with  the
  142.                value 16 for the base argument.  The corresponding
  143.                argument should be a pointer to unsigned integer.
  144.  
  145.           i    Matches an optionally signed integer, whose format
  146.                is  the  same as expected for the subject sequence
  147.                of the strtol() function with the value 0 for  the
  148.                base  argument.  The corresponding argument should
  149.                be a pointer to integer.
  150.  
  151.           n    No input is consumed.  The corresponding  argument
  152.                should be a pointer to integer into which is to be
  153.                written the number of  characters  read  from  the
  154.                input  stream  so far by the call to the function.
  155.                Execution of a %n directive does not increment the
  156.                assignment  count  returned  at  the completion of
  157.                execution of the function.
  158.  
  159.           e,f,g
  160.                Matches  an  optionally  signed   floating   point
  161.                number,  whose  format is the same as expected for
  162.                the subject string of the  strtod  function.   The
  163.                corresponding  argument  should  be  a  pointer to
  164.                floating.
  165.  
  166.           s    A character string is expected; the  corresponding
  167.                argument should be a character pointer pointing to
  168.                an array of characters large enough to accept  the
  169.                string  and  a terminating \0, which will be added
  170.                automatically.  The input field is terminated by a
  171.                white-space character.
  172.  
  173.           ws   A  wide  character   string   is   expected;   the
  174.                corresponding  argument should be a wide character
  175.                pointer pointing to an array  of  wide  characters
  176.                large  enough  to accept the wide character string
  177.                and a terminating \0, which will be added automat-
  178.                ically.   The  input  field  is  terminated  by  a
  179.                white-space character.
  180.  
  181.           c    Matches a sequence of  characters  of  the  number
  182.                specified  by the field width (1 if no field width
  183.                is present in the directive).   The  corresponding
  184.                argument  should be a pointer to the initial char-
  185.                acter of an  array  large  enough  to  accept  the
  186.                sequence.  No null character is added.  The normal
  187.                skip over white space is suppressed.
  188.  
  189.           wc   Matches a  sequence  of  wide  characters  of  the
  190.                number specified by the field width (1 if no field
  191.                width  is  present   in   the   directive).    The
  192.                corresponding  argument should be a pointer to the
  193.                initial character of  an  array  large  enough  to
  194.                accept  the sequence.  No null character is added.
  195.                The normal skip over white space is suppressed.
  196.  
  197.           [    Matches a nonempty sequence of characters  from  a
  198.                set  of  expected  characters  (the scanset).  The
  199.                corresponding argument should be a pointer to  the
  200.                initial  character  of  an  array  large enough to
  201.                accept the sequence and a terminating null charac-
  202.                ter,  which  will  be  added  automatically.   The
  203.                conversion specifier includes all subsequent char-
  204.                acters  in  the format string, up to and including
  205.                the matching right bracket  (]).   The  characters
  206.                between  the  brackets (the scanlist) comprise the
  207.                scanset,  unless  the  character  after  the  left
  208.                bracket  is  a  circumflex  (^), in which case the
  209.                scanset contains all characters that do not appear
  210.                in  the  scanlist  between  the circumflex and the
  211.                right bracket.  If the conversion specifier begins
  212.                with  [] or [^], the right bracket character is in
  213.                the scanlist and the next right bracket  character
  214.                is  the  matching  right  bracket  that  ends  the
  215.                specification; otherwise the first  right  bracket
  216.                character is the one that ends the specification.
  217.  
  218.                A range  of  characters  in  the  scanset  may  be
  219.                represented  by  the  construct first - last; thus
  220.                [0123456789] may be expressed [0- 9].  Using  this
  221.                convention,  first  must be lexically less than or
  222.                equal to last, or else the  dash  will  stand  for
  223.                itself. The character - will also stand for itself
  224.                whenever it is the first or the last character  in
  225.                the  scanlist.  To include the right bracket as an
  226.                element of the scanset,  it  must  appear  as  the
  227.                first  character  (possibly  preceded by a circum-
  228.                flex) of the scanlist and in this case it will not
  229.                be   syntactically   interpreted  as  the  closing
  230.                bracket.  At least one character  must  match  for
  231.                this conversion to be considered successful.
  232.  
  233.           p    Matches   the   set   of    implementation-defined
  234.                sequences  produced as output by the %p conversion
  235.                of the  printf(3S)  function.   The  corresponding
  236.                argument  should  be  a  pointer  to void.  If the
  237.                input item is a value converted earlier during the
  238.                same  program  execution, the pointer that results
  239.                compares  equal  to  that  value;  otherwise,  the
  240.                behavior of the %p conversion is undefined.
  241.  
  242.           If an invalid conversion character follows the  %,  the
  243.           results of the operation may not be predictable.
  244.  
  245.           The conversion specifiers E, G, and X  are  also  valid
  246.           and,  under  the  -Xa  and  -Xc  compilation modes (see
  247.           cc(1B)), behave the same as e, g, and x,  respectively.
  248.           Under  the -Xt compilation mode, E, G, and X behave the
  249.           same as le, lg, and lx, respectively.
  250.  
  251.           Each function  allows  for  detection  of  a  language-
  252.           dependent  decimal point character in the input string.
  253.           The decimal point character is defined by the program's
  254.           locale (category LC_NUMERIC).  In the "C" locale, or in
  255.           a locale where  the  decimal  point  character  is  not
  256.           defined,  the  decimal  point  character  defaults to a
  257.           period (.).
  258.  
  259.           The scanf() conversion terminates at end  of  file,  at
  260.           the end of the control string, or when an input charac-
  261.           ter conflicts with the control string.
  262.  
  263.           If end-of-file is encountered during input,  conversion
  264.           is  terminated.  If end-of-file occurs before any char-
  265.           acters matching the current directive  have  been  read
  266.           (other than leading white space, where permitted), exe-
  267.           cution of the  current  directive  terminates  with  an
  268.           input  failure;  otherwise,  unless  execution  of  the
  269.           current  directive  is  terminated  with   a   matching
  270.           failure,  execution of the following directive (if any)
  271.           is terminated with an input failure.
  272.  
  273.           If conversion terminates on a conflicting input charac-
  274.           ter,  the  offending  input character is left unread in
  275.           the input  stream.   Trailing  white  space  (including
  276.           new-line characters) is left unread unless matched by a
  277.           directive.   The  success  of   literal   matches   and
  278.           suppressed  assignments  is  not  directly determinable
  279.           other than via the %n directive.
  280.  
  281. RETURN VALUES
  282.      These routines return the number of successfully matched and
  283.      assigned  input  items; this number can be 0 in the event of
  284.      an early matching failure between an input character and the
  285.      control string.  If the input ends before the first matching
  286.      failure or conversion, EOF is returned.
  287.  
  288. EXAMPLES
  289.      The call to the function scanf():
  290.  
  291.           int i, n; float x; char name[50];
  292.           n = scanf ("%d%f%s", &i, &x, name);
  293.  
  294.      with the input line:
  295.  
  296.           25 54.32E-1 thompson
  297.  
  298.      will assign to n the value 3, to i the value 25,  to  x  the
  299.      value 5.432, and name will contain thompson\0.
  300.  
  301.      The call to the function scanf():
  302.  
  303.           int i; float x; char name[50];
  304.           (void) scanf ("%2d%f%*d %[0-9]", &i, &x, name);
  305.  
  306.      with the input line:
  307.  
  308.           56789 0123 56a72
  309.  
  310.      will assign 56 to i, 789.0 to x, skip 0123,  and  place  the
  311.      characters 56\0 in name.  The next character read from stdin
  312.      will be a.
  313.  
  314.